<template>
  <div>
    <div :class="{fullscreen:fullscreen}" class="tinymce-container" style="width:1000px">
      <textarea :id="tinymceId" class="tinymce-textarea" />
    </div>
  </div>
</template>

<script>
//const tinymceCDN = require('@/components/Tinymce/tinymce.min')
 const tinymceCDN = 'https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js'
import plugins from '@/components/Tinymce/plugins'
import toolbar from '@/components/Tinymce/toolbar'
import load from '@/components/Tinymce/dynamicLoadScript'
export default {
  name: "Tinymce",
  props : {
    value: {
      type: String,
      default: ''
    }
  },
  watch: {
    value(val) {
      console.log("val:",val)
      if (!this.hasChange && this.hasInit) {
        this.$nextTick(() =>
          window.tinymce.get(this.tinymceId).setContent(val || ''))
      }
    }
  },
  data() {
    return {
      hasChange: false,
      hasInit: false,
      tinymceId : 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + ''),
      fullscreen: false,
      toolbar : [],
      content : ''
    }
  },
  created() {
    this.init();
  },

  methods: {
    init() {
      // dynamic load tinymce from cdn
      load(tinymceCDN, (err) => {
        if (err) {
          console.error(err.message)
          return
        }
        //window.tinymce.baseURL = window.location.origin + '/tinymce'
        this.initTinymce()
      })
    },
    initTinymce() {
      const _this = this
      window.tinymce.init({
        language: 'zh_CN',
        selector: `#${this.tinymceId}`,
        height: '300px',
        body_class: 'panel-body ',
        object_resizing: false,
        toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
        menubar: '',
        plugins: plugins,
        end_container_on_empty_block: true,
        powerpaste_word_import: 'clean',
        code_dialog_height: 450,
        code_dialog_width: 1000,
        advlist_bullet_styles: 'square',
        advlist_number_styles: 'default',
        default_link_target: '_blank',
        link_title: false,
        nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
        init_instance_callback: editor => {
          if (_this.value) {
            editor.setContent(_this.value)
          }
          _this.hasInit = true
          editor.on('NodeChange Change KeyUp SetContent', () => {
            this.hasChange = true
            this.$emit('input', editor.getContent())
          })
        },
        setup(editor) {
          console.log("*****************")
          editor.on('FullscreenStateChanged', (e) => {
            _this.fullscreen = e.state
          })
        }
      })
    },
    setContent(value) {
      window.tinymce.get(this.tinymceId).setContent(value)
    },
    getContent() {
      window.tinymce.get(this.tinymceId).getContent()
    },
    destroyTinymce() {
      const tinymce = window.tinymce.get(this.tinymceId)

      if (tinymce) {
        tinymce.destroy()
      }
    },
  },
  mounted() {
    this.init()
  },
  activated() {
    if (window.tinymce) {
      this.initTinymce()
    }
  },
  deactivated() {
    this.destroyTinymce()
  },
  // destroyed() {
  //   this.destroyTinymce()
  // },
  unmounted() {
    this.destroyTinymce()
  }
}
</script>
<style scoped>
</style>
